package com.test.action;


import java.util.List;

import com.opensymphony.xwork2.ActionContext;
import com.test.base.BaseAction;
import com.test.domain.Forum;
import com.test.domain.Reply;
import com.test.domain.Topic;
/**
 * 主题
 * @author Administrator
 *
 */
public class TopicAction extends BaseAction<Topic>{
	private static final long serialVersionUID = 1L;
	private Integer forumId;
	
	public Integer getForumId() {
		return forumId;
	}
	public void setForumId(Integer forumId) {
		this.forumId = forumId;
	}
	public String addUI(){
		Forum forum = forumService.findForumById(forumId);
		ActionContext.getContext().put("forum", forum);
		return "addUI";
	}
	public String add(){
		Forum forum = forumService.findForumById(forumId);
		
		Topic topic = new Topic();
		topic.setTitle(model.getTitle());
		topic.setContent(model.getContent());
		topic.setForum(forum);
		//只能在显示层 获得的数据
		topic.setAuthor(getCurrentUser());
		topic.setIpAddress(getRequestIp());
		topicService.addTopic(topic);
		//应该只是处理请求提交的数据（表单提交的数据）
		/*topic.setPostTime(new Date());
		topic.setLastUpdateTime(topic.getPostTime());
		topicService.addTopic(topic);*/
		
		//每新发表一个主题，在对应的版块下，都要更新其主题数量,但是不太适合放在这里
		/*forum.setTopicCount(forum.getTopicCount()+1);
		forum.setLastTopic(topic);
		forum.setArticleCount(forum.getArticleCount()+1);
		forumService.updateForum(forum);*/
		
		
		//添加完主题后，要跳转到新发表主题的显示页面，所以要传入当前的topicId
		ActionContext.getContext().put("topicId", topic.getId());
		return "toShow";
	}
	public String show(){
		System.out.println("主题展示页面传过来的id值为："+model.getId());
		Topic topic = topicService.findTopicById(model.getId());
		ActionContext.getContext().put("topic", topic);
		
		List<Reply> replyList = replyService.findRepliesByTopic(topic);
		ActionContext.getContext().put("replyList", replyList);
		return "show";
	}
}
